﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Framework.DataDictionary.Agents;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.Utility;
using Sector7.Framework.Enums;
using Sector7.Framework.Consts;


namespace Sector7.Framework.DataDictionary.Windows
{
    /// <summary>
    /// DataMoveWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DataMoveWindow : AddModeWindowBase
    {
        #region Accessor
        #endregion

        #region Public functions
        public List<S7_ValueDomain> GetValueDomainList()
        {
            return m_valueDomainList;
        }
        #endregion

        #region Private functions
        /// <summary>
        /// 显示错误信息
        /// </summary>
        private void ShowErrors()
        {
            StringBuilder errors = new StringBuilder();
            foreach (KeyValuePair<string, string> kvp in m_errors)
            {
                errors.Append(string.Format("{0}: {1}\n", kvp.Key, kvp.Value));
            }

            Alert.ShowDialog(AlertType.Error, errors.ToString());
        }

        /// <summary>
        /// 数据迁移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DatabaseMoveClick(object sender, RoutedEventArgs e)
        {
            if (!CheckData())
            {
                ShowErrors();
                return;
            }

            string connectionString = string.Format("server={0};database=sector7;uid={1};pwd={2}",
                C_DatabaseInfo.TDBADDRESS, C_DatabaseInfo.DBUSERNAME, C_DatabaseInfo.DBPASSWORD);

            int ret = SystemServiceAgent.InitiallizeDatabaseTable(connectionString, m_tableList);

            if (0 == ret)
            {
                Alert.ShowDialog(AlertType.Notification, "数据迁移成功！");
            }
            else
            {
                Alert.ShowDialog(AlertType.Notification, "数据迁移失败！");
            }
        }

        /// <summary>
        /// 获取所有值域
        /// </summary>
        protected override void GetData()
        {
            m_valueDomainList = ValueDomainServiceAgent.GetAll();

            base.GetData();
        }

        //将所有值域填充到cbbValueDomain
        private void AddModeWindowBase_Loaded(object sender, RoutedEventArgs e)
        {
            GetData();
            cbbValueDomain.ItemsSource = m_valueDomainList;
        }


        /// <summary>
        /// 验证数据合法性
        /// </summary>
        /// <returns></returns>
        protected override bool CheckData()
        {
            m_errors.Clear();

            int Index = 0;
            m_tableName = txbTableName.Text;
            Index = cbbValueDomain.SelectedIndex;

            if (string.IsNullOrEmpty(m_tableName))
            {
                m_errors.Add("表名", "不能为空");
            }

            if (Index < 0) 
            {
                m_errors.Add("值域", "不能为空");
            }

            return base.CheckData();
        }

        private void AddDataInfo(object sender, RoutedEventArgs e)
        {
            if (!CheckData())
            {
                ShowErrors();
                return;
            }

            S7_DatabaseTable databaseTable = new S7_DatabaseTable();
            databaseTable.TableName = txbTableName.Text;
            databaseTable.ValueDomainId = (cbbValueDomain.SelectedItem as S7_ValueDomain).DomainId;
            m_tableList.Add(databaseTable);
            ClientUtil.LoadItemsSource(lsbDatabaseInfo, m_tableList);

            base.AddData();
        }

        private void DelDataInfo(object sender, RoutedEventArgs e)
        {
            var selectedItem = lsbDatabaseInfo.SelectedItem as S7_DatabaseTable;
            if (selectedItem != null)
            {
                m_tableList.Remove(selectedItem);
                ClientUtil.LoadItemsSource(lsbDatabaseInfo, m_tableList);
            }
            base.AddData();
        }

        #endregion

        #region Structure
        public DataMoveWindow()
        {
            InitializeComponent();
            //加载所有值域
            GetData();
        }
        #endregion

        #region Inner members
        private string m_tableName;
        private string m_valueDomianId;
        private List<S7_DatabaseTable> m_tableList = new List<S7_DatabaseTable>();
        private List<S7_ValueDomain> m_valueDomainList = null;
        #endregion
    }
}
